﻿var ulMessage = "";
var CityID = "";
function initTrademark() {

    CityID = document.getElementById("CityID").value;

    var CategoryID = document.getElementById("categoryID").value;
    //通过页面的URL得到需要的数据
    var locationUrl = window.location.href;
    var arrUrl = locationUrl.split("/");
    var arr = arrUrl[arrUrl.length - 1];
    //得到商户ID
    var TrademarkID = arr.split('?');
    var arrTrademarkID = TrademarkID[1].split('&');
    var CityID1 = arrTrademarkID[0];
    var arrCityID = CityID1.split('=');
    //得到城市ID
    CityID = arrCityID[1];
    //对页眉上的几个标签进行连接赋值
    document.getElementById("backCity").href = '../../City/CityDetail/' + CityID;
    document.getElementById("backCountry").href = '../../Home/Index';
    document.getElementById("backCategory").href = '../../City/CityDetail/' + CityID;

    var lan = null;
    document.getElementById("testS").value = CityID;
    //对所有的方法进行初始化加载
    getTrademark(TrademarkID[0], CityID);
    getHotCity(TrademarkID[0]);
    getSubShopSub(TrademarkID[0], CityID, CategoryID);
    readCommentary(TrademarkID[0]);
    getCityWeather(CityID);
    getTrademarkImg(TrademarkID[0]);
    getShopVoucher(TrademarkID[0]);
    getCityImg(CityID);
    getCityMessage(CityID);
    
    var LatLng = TrademarkID[1].split('&');
    //得到商户所在的经纬度
    var lat = LatLng[LatLng.length - 2].split('=');

    var lng = LatLng[LatLng.length - 1].split('=');
    var shopMapImgsrc = "";
    //判断经纬度如果为空或为0的时候就把商户地址展示图设置为默认无图片显示
    if ((lat[1] == '0' && lng[1] == '0') || (lat[1] == "" && lng[1] == "") || (lat[1] == "0.00" && lng[1] == "0.00")) {
        shopMapImgsrc = "/Content/img/home/iniImg.bmp";

    } else {
        shopMapImgsrc = "http://maps.google.com/staticmap?center=" + lat[1] + "," + lng[1] + "&zoom=16&size=250x150&maptype=mobile\&markers=" + lat[1] + "," + lng[1] + ",red&key=ABQIAAAAWPpetO1Ar6eiW-f933fXvRTfDV4YbxXmyZ7S6XIpnYVyS6_XwRQxmHGgg9O4oA5HFdsGDNqYIrgVsw&sensor=false";

    }

    document.getElementById("shopMapImg").src = shopMapImgsrc;


    //对更多图片进行连接赋值
    document.getElementById("moreImg").href = '../../City/MoreCityImg/' + TrademarkID[0] + "?CityID=" + CityID + "&CategoryID=" + CategoryID + "&Lat=" + lat[1] + "&Lng=" + lng[1];
    //对点评进行连接赋值
    document.getElementById("CommentaryAction").action = '/City/Commentary/' + TrademarkID[0] + "?CityID=" + CityID + "&CategoryID=" + CategoryID + "&Lat=" + lat[1] + "&Lng=" + lng[1];
    //对takeCommentary进行连接赋值
    document.getElementById("takeCommentary").href = '/City/Commentary/' + TrademarkID[0] + "?CityID=" + CityID + "&CategoryID=" + CategoryID + "&Lat=" + lat[1] + "&Lng=" + lng[1];

    $(function() {

        $('#links tr').biggerlink();

    })

}

//得到商户信息
function getTrademark(TrademarkID, CityID) {
    //通过JSON来得到相应的商户信息
    $.getJSON('/City/getTrademark/' + TrademarkID + "," + CityID, { t: new Date() }, function(j) {

        var arr = new Array();

        for (var i = 0; i < j.length; i++) {

            var values = j[i];
            // alert(values);
            arr = values.split('★');
            //alert(arr.length);
            //得到商户的LOGO名
            var shopLogo = arr[0];
            //得到商户的中文名
            var chineseName = arr[1];
            //得到商户的日文名
            var japonicName = arr[6];
            //判断商户是否有日文名
            if (japonicName == "" || japonicName == null) {
                japonicName = '无';
            }
            //得到商户的英文名
            var englishName = arr[8];
            //得到商户的官方网站
            var officialweb = arr[4];
            //得到商户的地址
            var shopadd = arr[2];
            //得到商户的交通信息
            var shoptraffic = arr[5];
            //得到商户的营业时间
            var businesshours = arr[9];

            //得到商户的联系方式
            var tel = arr[7];
            //得到商户的详细信息
            var message = arr[3];
            //把得到的所有信息添加到相应的标签中
            document.getElementById("shopLogo").src = '/Content/img/shop/81/' + TrademarkID + '/' + shopLogo;
            $("#chineseName").html(chineseName);
            $("#shopName").html(chineseName);
            $("#englishName").html(englishName);
            $("#japonicName").html(japonicName);
            $("#businessHours").html(businesshours);
            $("#shopAdd").html(shopadd);
            $("#shoptraffic").html(shoptraffic);
            $("#tel").html(tel);
            $("#officialweb").html(officialweb);
            $("#shopMessage").html(message);
            //$("#officialweb").attr("href", '../../../../www.hakuhinkan.co.jp ');
        }

    })
}

//得到商户的商品和商品图片
function getTrademarkImg(TrademarkID) {
    var obj = "";
    //通过JSON来得到相应商户的图片信息
    $.getJSON('/City/getShopImage/' + TrademarkID, { t: new Date() }, function(j) {
        obj = j;
        var arr = new Array();
        //判断查询是否有数据
        if (j[0] == " " || j[0] == null) {
            //alert("test");
            document.getElementById("wrap").style["visibility"] = "hidden";
            document.getElementById("lb_Img").innerHTML = "暂无任何商品信息!";
        } else {
            document.getElementById("wrap").style["visibility"] = "visible";
            document.getElementById("lb_Img").style["visibility"] = "hidden";
            for (var i = 0; i < j.length; i++) {

                var value = j[i];
                //alert(value);
                arr = value.split('|');
                //得到商户ID
                var shopID = arr[1];
                //得到IMG的NAME
                var fileName = arr[0];
                //得到IMG的描述
                var imgMessage = arr[2];
                //进行信息赋值
                var message = "<a href='/Content/img/shop/81/" + shopID + "/" + fileName + "' class='thickbox'><img id='shopImg1' src='/Content/img/shop/81/" + shopID + "/" + fileName + "' style='height: 125px; width: 140px' alt='' /></a>";
                //把相关的信息添加到相应的标签中然后在页面上显示出来
                var ul = document.getElementById("mycarousel");
                var li = document.createElement("li");

                li.innerHTML = message;

                ul.appendChild(li);
            }



            //得到页面效果的JS
            function mycarousel_initCallback(carousel) {

                carousel.buttonNext.bind('click', function() {
                    carousel.startAuto(0);
                });

                carousel.buttonPrev.bind('click', function() {
                    carousel.startAuto(0);
                });

                carousel.clip.hover(function() {
                    carousel.stopAuto();
                }, function() {
                    carousel.startAuto();
                });
            };

            jQuery('#mycarousel').jcarousel({
                auto: 10,
                wrap: 'last',
                initCallback: mycarousel_initCallback
            });


            $(function() {
                $('#mycarousel a').lightBox({ fixedNavigation: true });
            });


        }

    })

}


//得到子店的信息
function getSubShopSub(TrademarkID, CityID, CategoryID) {
    //把商户ID通过JSON到后台进行查询
    $.getJSON('/City/ShopSubShop/' + TrademarkID, { t: new Date() }, function(json) {

        var arr = new Array();
        for (var i = 0; i < json.length; i++) {
            //得到查询出的信息
            var shopsubshopvalues = json[i];
            arr = shopsubshopvalues.split('|');

            if (arr != ",") {
                //得到相对应的子商户名,并通过调用getSubShopSublanlng方法来给各个子商户进行连接赋值
                $("#shopsubshop1").html(arr[0]);

                document.getElementById("shopsubshop2").href = arr[1];
                var shopTypeID = 'shopsubshop2';

                getSubShopSublanlng(arr[1], CityID, CategoryID, shopTypeID);

                $("#shopsubshop3").html(arr[2]);

                document.getElementById("shopsubshop4").href = arr[3];
                var shopTypeID1 = 'shopsubshop4';
                getSubShopSublanlng(arr[3], CityID, CategoryID, shopTypeID1);


                $("#shopsubshop5").html(arr[4]);

                document.getElementById("shopsubshop6").href = arr[5];

                var shopTypeID2 = 'shopsubshop6';

                getSubShopSublanlng(arr[5], CityID, CategoryID, shopTypeID2);
                $("#shopsubshop7").html(arr[6]);

                document.getElementById("shopsubshop8").href = arr[7];
                var shopTypeID3 = 'shopsubshop8';
                getSubShopSublanlng(arr[7], CityID, CategoryID, shopTypeID3);
            } else {
                //如果查询出的商户没有子商户的赋值
                $("#shopsubshop1").html('暂无任何信息');
                $("#shopsubshop3").html('暂无任何信息');
                $("#shopsubshop5").html('暂无任何信息');
                $("#shopsubshop7").html('暂无任何信息');
                document.getElementById("shopParticular1").innerHTML = "";
                document.getElementById("shopParticular1").width = "20%";
                document.getElementById("shopParticular2").innerHTML = "";
                document.getElementById("shopParticular3").innerHTML = "";
                document.getElementById("shopParticular4").innerHTML = "";
            }

        }


        $('ul.toggler-1').toggleElements();
        $('ul.toggler-2').toggleElements()
        $('ul.toggler-3').toggleElements(); ;


    })
}

//得到子商户的经纬度
function getSubShopSublanlng(TrademarkID, CityID, CategoryID, shopTypeID) {
    //通过JSON来得到相应子商户的经纬度
    $.getJSON('/City/SubShopSublanlgn/' + TrademarkID + "," + CityID + "," + CategoryID, { t: new Date() }, function(j) {
        var arr = new Array();
        for (var i = 0; i < j.length; i++) {

            var shopsubshopvalues = j[i];

            arr = shopsubshopvalues.split('|');
            //把查询出的子商户的完整连接添加到相应的标签中
            var shop2 = document.getElementById(shopTypeID).href;
            document.getElementById(shopTypeID).href = shop2 + '?CityID=' + CityID + '&CategoryID=' + CategoryID + '&Lat=' + arr[0] + '&Lng=' + arr[1];

        }
    })
}


//读取评论内容
function readCommentary(TrademarkID) {
    var s = "";
    //通过JSON来得到该商户的评论内容
    $.getJSON('/City/readCommentary/' + TrademarkID, { t: new Date() }, function(j) {
        //当查询出有评论内容时
        if (j.length != 0 || j[i] != '尚没有评论!') {
            for (var i = 0; i < j.length; i++) {
                //得到评论的内容

                var value = j[i];

                var arr = value.split('|');
                if (arr[0] != "尚没有评论") {
                    var tr1 = document.getElementById("testTable").insertRow();
                    var td1 = tr1.insertCell();

                    td1.align = 'center';
                    //把评论的内容显示在页面上
                    td1.innerHTML = "<table><tr><td  rowspan='2' align='center' style='border:2px solid #FFE9CA' width='18%'><table cellpadding='0' cellspacing='0' width='30%'><tr><td><img src='../../Content/img/commentary/image1.gif' /></td></tr><tr><td>网友用户名：" + arr[0] + "</td></tr></table></td><td width='100%'><table cellpadding='0' cellspacing='0' width='100%'><tr><td style='border:2px solid #FFE9CA'>次评论发表于：" + arr[1] + "</td></tr><tr><td class='style10'  style='border:2px solid #FFE9CA' align='left'>" + arr[2] + "</td></tr></table></td></tr></table>";
                } else {
                    document.getElementById("testTable").style["visibility"] = "hidden";
                    $("#noCommentary").html('暂无任何评论内容!');
                }
            }
        }
        else {
            document.getElementById("testTable").style["visibility"] = "hidden";
            $("#noCommentary").html('暂无任何评论内容!');

        }
    })
}

//得到城市天气
function getCityWeather(ID) {

    var param = "/Util/getCityWeather/" + ID;
    //通过JSON来得到该城市的天气信息内容
    $.getJSON(param, { t: new Date() }, function(j) {

        var arr = new Array();

        for (var i = 0; i < j.length; i++) {
            //得到天气信息内容以后对其进行赋值.然后显示在页面上
            var value = j[i];
            document.getElementById("WeatherCity" + (i + 1)).innerHTML = value;
        }

    });
}

//得到商户优惠信息
function getShopVoucher(ID) {
    //通过JSON来得到该商户的优惠信息
    var param = "/City/getShopVoucher/" + ID;
    $.getJSON(param, { t: new Date() }, function(j) {

        var arr = new Array();

        var ul = document.getElementById("li_Voucher");
        for (var i = 0; i < j.length; i++) {

            var value = j[i];
            arr = value.split('|');
            //得到优惠信息内容
            var voucherDescription = arr[0];
            //得到优惠信息名称
            var voucherName = arr[1];
            //得到优惠信息开始时间
            var voucherStartDate = arr[2];
            //得到优惠信息结束时间
            var voucherEndDate = arr[3];
            //把所有的优惠信息连接成一个长字符串
            var message = voucherDescription + voucherName + voucherStartDate + voucherEndDate;
            //判断查询出的内容是否为空
            if (value == null || value == "" || value == " ") {
                document.getElementById("ShopVoucher_tb").style["visibility"] = "hidden";
                document.getElementById("lb_ShopVoucher").innerHTML = '暂无任何优惠信息!';
            } else {
                //当不为空的时候就把查询出的优惠信息内容显示在页面上
                document.getElementById("ShopVoucher_tb").style["visibility"] = "visible";
                document.getElementById("lb_ShopVoucher").style["visibility"] = "hidden";

                var li = document.createElement("li");

                var t = document.createTextNode(message);

                li.appendChild(t);
                ul.appendChild(li);

            }
        }

    });
}

//得到商户的图片
function getCityImg(CityID) {
    //根据CITYID调用JSON得到相应的城市图片
    $.getJSON('/Util/getCityImg/' + CityID, { t: new Date() }, function(j) {
        var value = j[0];
        if (value == 'no image') {
            document.getElementById("testCityImg").innerHTML = "";
        } else {
            document.getElementById("testCityImg").innerHTML = "<img width='60px' height='60px' id='img_CityImg' src = '" + value + "'/>";
        }
    })
}


//得到城市信息
function getCityMessage(CityID) {
    var url = "/City/GetCityMessage/" + CityID;
    //调用JSON得到详细信息
    $.getJSON(url, { t: new Date() }, function(j) {

        var arr = new Array();

        for (var i = 0; i < j.length; i++) {
            var vaule = j[i];
            arr = vaule.split('|');
            //得到城市中文名
            var cityNameCH = arr[0];
            //得到城市英文名
            var cityNameEN = arr[2];
            //得到城市日文名
            var cityNameJP = arr[3];
            //把得到的相应数据填充到相应的标签里
            document.getElementById("cityNameCH").innerHTML = cityNameCH;
            document.getElementById("cityNameEN").innerHTML = cityNameEN;
            document.getElementById("cityNameJP").innerHTML = cityNameJP;
            // document.getElementById("CountryName").innerHTML = '日本';
            // document.getElementById("CityName").innerHTML = cityNameCH;
            //得到城市详细信息
            var cityMessage = arr[4];
            //初始化信息的字数
            var num = 55;
            //记录最后结束字数
            var sum = 1;
            //对城市信息进行分开显示
            for (var k = 0; k < cityMessage.length; k++) {
                //对信息进行SUBSTRING,得到相应字数数据
                var arrCityMessage = cityMessage.substring(k, num);

                ulMessage += arrCityMessage + "|";
                //得到每次SUBSTRING的开始字数
                k += 54;
                //得到每次SUBSTRING的结束字数
                if (cityMessage.length - num < 55) {

                    num = cityMessage.length;

                } else {
                    num = k + 56;
                }
                sum += 1;

            }
            var arrulMessage = ulMessage.split('|');
            //动态生成相应的LI标签 
            var ul = document.getElementById("testLiMessage");

            for (var b = 0; b < arrulMessage.length - 1; b++) {

                var li = document.createElement("li");
                var t = document.createTextNode(arrulMessage[b]);
                li.appendChild(t);
                ul.appendChild(li);
            }
            //将填充好数据的子标签添加到相应的父标签中
            $('#testLiMessage').innerfade({
                animationtype: 'fade',
                speed: 750,
                timeout: 4000,
                type: 'random',
                containerheight: '1em'
            });
        }


    });
    //得到城市信息以后把生成标签

}

//商户详细信息页中的查询按钮功能
function getTrademarkSearch() {
    //得到用户选择的选项的相对应的CATEGORYID
    var categoryID = document.getElementById("Category").value;

    var parm = "/City/getTrademarkSearch/" + CityID + "," + categoryID;

    $.getJSON(parm, { t: new Date() }, function(j) {
        //得到查询后的值
    var value = j[0];
    //alert(value);
        //如果查询后的值为YES的话,进行跳转页面操作
        if (value == 'ok') {
            
            window.navigate('/City/CityDetail/' + CityID);
        }
    })
}

//商户详细信息页中的搜索按钮功能
function getTrademarkSearchButton() {

    //得到用户输入的用于查询的字符串
    var searchString = document.getElementById("search").value;

    var parm = "/City/getTrademarkSearchButton/" + searchString;

    $.getJSON(parm, { t: new Date() }, function(j) {
        //得到查询后的值
        var value = j[0];

        //如果查询后的值为YES的话,进行跳转页面操作
        if (value == 'ok') {

            //window.open('/City/CityDetail/' + CityID, 'test', 'height=100, width=400, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');

            window.navigate('/City/CityDetail/' + CityID);
        }
    })
}

//点击官方网站后的条转
function openTrademarkWeb() {
    //alert();
    //$("#officialweb").attr("href", document.getElementById("officialweb").innerHTML);
    var s = document.getElementById("officialweb").innerHTML;
    var st = s.split(':');
    if (st[0] != 'http') {
        s = 'http://' + document.getElementById("officialweb").innerHTML;
    }
    window.open(s, 'test', 'height=400, width=800, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');
}


//得到相关信息
function getHotCity(shopID) {

    var parm = '/City/trademarkGetCorrelationMessage/' + shopID;
    $.getJSON(parm, { t: new Date() }, function(j) {

        //数据变量初始化
        var value = "";
        var arr = new Array();

        for (var i = 0; i < j.length; i++) {
            //得到数据后对数据进行分割
            var value = j[i];
            arr = value.split('★');
            //得到shopid
            var shopID = arr[0];
            //得到shopName
            var shopName = arr[1];
            //得到shop经纬度
            var lat = arr[2];
            var lng = arr[3];
            //得到shop所在城市
            var shopCityID = arr[4];
            //得到shop的CATEGORYID
            var shopCategoryID = arr[5];
            //组合连接路径
            var rel = "/City/Trademark/" + shopID + "?CityID=" + shopCityID + "&CategoryID=" + shopCategoryID + "&Lat=" + lat + "&Lng=" + lng;
            //给相关的标签赋值
            $("#hotcityrel" + (i + 1)).attr("href", rel);
            document.getElementById("hotcityname" + (i + 1)).innerHTML = shopName;
           

        }
    })
}